<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 扁平化数组 */
      const arr = [1, [2, 3, [4, [5], [6], 7], [8], 9], 10];

      //数组自带的一个方法 flat
      /* const result = arr.flat(Infinity);
      console.log(result); */

      /* 
        自己不使用flat实现
      */

      function flat(arr) {
        const result = arr.reduce((p, c) => {
          //判断每次进来的值是否是数组
          if (Array.isArray(c)) {
            return [...p, ...flat(c)];
          } else {
            return [...p, c];
          }
        }, []);

        return result;
      }

      const result = flat(arr);
      console.log(result);
    </script>
  </body>
</html>
